package com.swings.swing;

import com.storere.entity.Baoxianzhonglei;
import org.springframework.stereotype.Component;


import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author hl
 * @Date 2023/4/19 2:26
 * @PackageName:com.ynnz.store.swing
 * @ClassName: InsureTypeFrame
 * @Description: 
 * @Version 1.0
 */
@Component
public class InsureTypeFrame extends ParentFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

//    private BaoxianzhongleiMapper baoxianzhongleiDao = new BaoxianzhongleiMapperImpl();


    private JTable table;

    private JButton addBtn;// 
    private JButton cleanBtn;// 
    private JButton delBtn;// 
    private JTextField nameTxt, mobileTxt;
    private JScrollPane listPanel;
    private JPanel baoxianzhongleiIdPanel;

    private int baoxianzhongleiId = 0;// tableId

    public InsureTypeFrame() {
        super("����������");
        init();// 
        baoxianzhongleiIdList();// 
        createbaoxianzhongleiIdForm();// 
        editbaoxianzhongleiIdEvent();// 
        delbaoxianzhongleiIdEvent();// 
        clearSelect();
        this.setResizable(false);
        this.setVisible(true);// 
    }

    /**
     * 
     *
     */
    private void init() {
        this.setLayout(null);
        this.setBounds(300, 150, 650, 450);// 
        delBtn = new JButton("����������");
        delBtn.setBounds(60, 330, 90, 20);
        this.add(delBtn);
    }

    /**
     * 
     */
    private void baoxianzhongleiIdList() {
        String[] head = { "�������id", "��������", "����ʱ��"};
//        List<Baoxianzhonglei> list = baoxianzhongleiDao.selectList(wrapper);
        List<Baoxianzhonglei> list=null;
        Object data[][] = new Object[0][head.length];
        if (list != null) {
            data = new Object[list.size()][head.length];
            int i = 0;
            for (Iterator<Baoxianzhonglei> iterator = list.iterator(); iterator.hasNext();) {
                Baoxianzhonglei baoxianzhonglei = (Baoxianzhonglei) iterator.next();
                data[i] = new Object[] { baoxianzhonglei.getId(), baoxianzhonglei.getBaoxianzhonglei(), baoxianzhonglei.getAddtime() };
                i++;
            }
        }
        table = new JTable(data, head);
        int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
        listPanel = new JScrollPane(table, v, h);
        listPanel.setBounds(10, 0, 400, 300);
        this.add(listPanel);
        // new table
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                int rowNo = table.getSelectedRow();// 
                TableModel model = table.getModel();// tableModel
                if (rowNo < 0) {// 
                    return;
                }
                baoxianzhongleiId = (int) model.getValueAt(rowNo, 0);

//                Baoxianzhonglei baoxianzhonglei = baoxianzhongleiDao.getBaoxianzhonglei(baoxianzhongleiId);
                Baoxianzhonglei baoxianzhonglei = null;
                if (baoxianzhonglei != null) {
                    nameTxt.setText(baoxianzhonglei.getBaoxianzhonglei());
                    mobileTxt.setText(String.valueOf(baoxianzhonglei.getId()));
                    addBtn.setText("");
                }
            }
        });

    }

    /**
     * 
     *
     */
    private void createbaoxianzhongleiIdForm() {
        baoxianzhongleiIdPanel = new JPanel();
        baoxianzhongleiIdPanel.setLayout(null);// 
        baoxianzhongleiIdPanel.setBounds(410, 0, 220, 350);
        this.add(baoxianzhongleiIdPanel);
        JLabel title = new JLabel("");
        title.setFont(new Font(Font.DIALOG, Font.BOLD, 14));
        title.setBounds(60, 10, 100, 20);
        baoxianzhongleiIdPanel.add(title);


        JLabel mobile = new JLabel("");
        mobile.setBounds(20, 70, 80, 20);
        mobileTxt = new JTextField();
        baoxianzhongleiIdPanel.add(mobile);
        mobileTxt.setBounds(105, 70, 80, 20);
        baoxianzhongleiIdPanel.add(mobileTxt);


        JLabel name = new JLabel("");
        name.setBounds(20, 40, 80, 20);
        nameTxt = new JTextField();
        nameTxt.setBounds(105, 40, 80, 20);
        baoxianzhongleiIdPanel.add(name);
        baoxianzhongleiIdPanel.add(nameTxt);

        addBtn = new JButton("");
        addBtn.setBounds(20, 240, 90, 20);
        cleanBtn = new JButton("");
        cleanBtn.setBounds(115, 240, 90, 20);
        baoxianzhongleiIdPanel.add(addBtn);
        baoxianzhongleiIdPanel.add(cleanBtn);

    }

    /**
     *  
     */
    public void closePa() {
        this.remove(listPanel);
    }

    /**
     * /
     */
    private void editbaoxianzhongleiIdEvent() {
        addBtn.addActionListener(new ActionListener() {
            private Pattern patter;
            private Matcher matcher;

            @Override
            public void actionPerformed(ActionEvent e) {
                Baoxianzhonglei baoxianzhonglei = new Baoxianzhonglei();
                String name = nameTxt.getText();
                if (name == null || name.trim().length() == 0) {
                    JOptionPane.showMessageDialog(null, "");
                    return;
                } else if (name.trim().length() > 20) {
                    JOptionPane.showMessageDialog(null, "20");
                    return;
                } else {
                    baoxianzhonglei.setBaoxianzhonglei(name);
                }
                String mobile = mobileTxt.getText();
                String mobileReg = "1\\d{10}";
                patter = Pattern.compile(mobileReg);
                matcher = patter.matcher(mobile);
                if (!matcher.matches()) {
                    JOptionPane.showMessageDialog(null, "111");
                    return;
                } else {
                    baoxianzhonglei.setId(Integer.valueOf(mobile));
                }
//                Baoxianzhonglei be = baoxianzhongleiDao.getBaoxianzhonglei(Integer.valueOf(mobile));
                Baoxianzhonglei be = null;
                if (be != null && baoxianzhongleiId == 0) {
                    JOptionPane.showMessageDialog(null, "");
                    return;
                }


                boolean ret = false;
                String msg = "";
                //todo 
                if (baoxianzhongleiId != 0) {// id
                    baoxianzhonglei.setId(baoxianzhongleiId);
//                    ret = baoxianzhongleiDao.update(baoxianzhonglei);
                    msg = "";
                } else {// 
//                    ret = baoxianzhongleiDao.addBaoxianzhonglei(baoxianzhonglei);
                    msg = "";
                }
                if (ret) {
                    JOptionPane.showMessageDialog(null, msg + "");
                    closePa();
                    baoxianzhongleiIdList();
                } else {
                    JOptionPane.showMessageDialog(null, msg + "");
                }
            }
        });
    }

    /**
     * 
     */
    private void delbaoxianzhongleiIdEvent() {

        delBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int rowNo = table.getSelectedRow();
                if (rowNo < 0) {
                    JOptionPane.showMessageDialog(null, "");
                    return;
                } else {
                    int sel = JOptionPane.showConfirmDialog(null, "");
                    if (sel == 0) {
//                        boolean ret = baoxianzhongleiDao.del(baoxianzhongleiId);
                        boolean ret = false;
                        if (ret) {
                            // 
                            table.getSelectionModel().clearSelection();
                            baoxianzhongleiId = 0;
                            nameTxt.setText("");
                            mobileTxt.setText("");
                            addBtn.setText("");
                            closePa();
                            baoxianzhongleiIdList();
                            JOptionPane.showMessageDialog(null, "");
                        } else {
                            JOptionPane.showMessageDialog(null, "");
                        }
                    }
                }
            }
        });
    }

    /**
     * 
     */
    private void clearSelect() {
        cleanBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int rowNo = table.getSelectedRow();
                if (rowNo < 0) {
                    return;
                }
                table.getSelectionModel().clearSelection();
                baoxianzhongleiId = 0;
                nameTxt.setText("");
                mobileTxt.setText("");
                addBtn.setText("");
            }
        });
    }
}
